package com.tpc.control.util;

import java.util.Hashtable;

import javax.faces.application.Application;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;

public class LdapUtil {
	private static String base = "dc=tpcgrp,dc=co,dc=th";
	private static String INITIAL_CONTEXT_FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
	private static String SECURITY_AUTHENTICATION = "simple";
	// private static String PROVIDER_URL = "ldap://192.168.42.217:389";
	private static String PROVIDER_URL = "ldap://192.168.42.235:389";

	public static boolean existingUserName(String userName)
			throws NamingException {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Application app = facesContext.getApplication();

		Hashtable srchEnv = new Hashtable(11);
		srchEnv.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
		srchEnv.put(Context.PROVIDER_URL, PROVIDER_URL);
		srchEnv.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);
		String[] returnAttribute = { "uid" };

		SearchControls srchControls = new SearchControls();
		srchControls.setReturningAttributes(returnAttribute);
		srchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		String searchFilter = "(uid=" + userName + ")";

		DirContext srchContext = new InitialDirContext(srchEnv);
		NamingEnumeration srchResponse = srchContext.search(base, searchFilter,
				srchControls);
		// Probably want to test for nulls here
		SearchResult sr = (SearchResult) srchResponse.nextElement();
		Attributes attrs = sr.getAttributes();
		System.out.println("DN: " + sr.getName());
		System.out.println("uid: " + attrs.get("uid").get());
		return true;

	}

	public static boolean verifyPassword(String userName, String password)
			throws Exception {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		Application app = facesContext.getApplication();

		Hashtable srchEnv = new Hashtable(11);

		srchEnv.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
		srchEnv.put(Context.PROVIDER_URL, PROVIDER_URL);
		srchEnv.put(Context.SECURITY_AUTHENTICATION, SECURITY_AUTHENTICATION);

		String[] returnAttribute = { "sambalmpassword", "uid" };
		SearchControls srchControls = new SearchControls();
		srchControls.setReturningAttributes(returnAttribute);
		srchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
		String searchFilter = "(uid=" + userName + ")";

		DirContext srchContext = new InitialDirContext(srchEnv);
		NamingEnumeration srchResponse = srchContext.search(base, searchFilter,
				srchControls);
		// Probably want to test for nulls here
		SearchResult sr = (SearchResult) srchResponse.nextElement();
		Attributes attrs = sr.getAttributes();
		System.out.println("DN: " + sr.getName());
		System.out.println("uid: " + attrs.get("uid").get());

		String sambalmpassword = (String) attrs.get("sambalmpassword").get();
		String inputPassword = Formatting.byteToString(SHAPassword
				.lmHash(password));
		if (sambalmpassword.equalsIgnoreCase(inputPassword)) {
			return true;
		} else {
			return false;
		}

	}
}
